package com.senlan.water.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senlan.water.common.constant.DeleteStatusEnum;
import com.senlan.water.common.exception.BusinessException;
import com.senlan.water.common.result.ResultCode;
import com.senlan.water.common.util.DateUtil;
import com.senlan.water.common.util.UserInfoUtil;
import com.senlan.water.dao.domain.CustomerAppeal;
import com.senlan.water.dao.domain.User;
import com.senlan.water.dao.service.CustomerAppealService;
import com.senlan.water.dao.mapper.CustomerAppealMapper;
import com.senlan.water.dto.appeal.CustomerAppealAddDTO;
import com.senlan.water.dto.appeal.CustomerAppealEditDTO;
import com.senlan.water.dto.appeal.CustomerAppealSearchDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 10499
* @description 针对表【customer_appeal(客户诉求)】的数据库操作Service实现
* @createDate 2024-12-03 15:25:29
*/
@Service
public class CustomerAppealServiceImpl extends ServiceImpl<CustomerAppealMapper, CustomerAppeal>
    implements CustomerAppealService{

    @Resource
    private UserInfoUtil userInfoUtil;

    @Override
    public Boolean addNew(CustomerAppealAddDTO addDTO) {
        CustomerAppeal customerAppeal = new CustomerAppeal();
        BeanUtil.copyProperties(addDTO,customerAppeal);
        if (CollectionUtil.isNotEmpty(addDTO.getFileList() )){
            customerAppeal.setPhotoUrl(addDTO.getFileList().get(0).getUrl());
        }
        customerAppeal.setDeleteFlag(DeleteStatusEnum.DELETE_STATUS_NORMAL.getCode());

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        customerAppeal.setCreateBy(userInfo.getNickname());
        customerAppeal.setUpdateBy(userInfo.getNickname());
        return baseMapper.insert(customerAppeal) > 0;
    }

    @Override
    public Boolean editById(CustomerAppealEditDTO editDTO) {
        CustomerAppeal customerAppeal = getById(editDTO.getId());
        if (customerAppeal == null){
            throw new BusinessException(ResultCode.APPEAL_DATA_NOT_EXITS);
        }
        BeanUtil.copyProperties(editDTO,customerAppeal);

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        customerAppeal.setUpdateBy(userInfo.getNickname());
        return baseMapper.updateById(customerAppeal) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        CustomerAppeal customerAppeal = getById(id);
        if (customerAppeal == null){
            throw new BusinessException(ResultCode.APPEAL_DATA_NOT_EXITS);
        }
        customerAppeal.setDeleteFlag(DeleteStatusEnum.DELETE_STATUS_DELETED.getCode());

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        customerAppeal.setUpdateBy(userInfo.getNickname());
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public Page<CustomerAppeal> getPage(CustomerAppealSearchDTO searchDTO) {
        LambdaQueryWrapper<CustomerAppeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(searchDTO.getAppealType() != null,CustomerAppeal::getAppealType,searchDTO.getAppealType());
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getName()),CustomerAppeal::getName,searchDTO.getName());
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getPhone()),CustomerAppeal::getPhone,searchDTO.getPhone());

        if (CollectionUtil.isNotEmpty(searchDTO.getCreateTime())){
            queryWrapper.ge(CustomerAppeal::getCreateTime,searchDTO.getCreateTime().get(0));
            queryWrapper.le(CustomerAppeal::getCreateTime, DateUtil.convertDate(searchDTO.getCreateTime().get(1)));
        }

        Page<CustomerAppeal> page = new Page<>();
        page.setCurrent(searchDTO.getPageNum());
        page.setSize(searchDTO.getPageSize());
        baseMapper.selectPage(page,queryWrapper);
        return page;
    }
}




